var http = require('http'),
  url = require('url'),
  fs = require('fs'),
  util = require('util'),
  querystring = require('querystring');

global.BASE_DIR = __dirname;

http.createServer(function(req, res) {
  var pathname = url.parse(req.url).pathname; // '/hello?c=doAction'
  if (pathname === '/favicon.ico') {
    return;
  }
  var module = pathname.substr(1), // 'hello'
      str = url.parse(req.url).query,// 'c=doAction'
      controller = querystring.parse(str).c,// 'doAction'
      classObj = ''; // 'doAction'
  try{
    classObj = require('./modules/'+module);
  }catch(err){
    console.error('error: '+err);
  }
  if (classObj) {
    classObj.init(res,req);      // hello.init();
    classObj[controller].call(); // hello.doAction()
  }else{
    res.writeHeand(404,{'Content-Type':'text/plain'});
    res.end('can not find source');
  }
}).listen(4000, '127.0.0.1');
console.log('server start at http://localhost:3000');